package com.itheima.service;

import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//业务层 写业务逻辑，什么都在表现层写的话就会很杂乱
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    public boolean save(Book book) {
        return bookDao.save(book) > 0;
    }

    public boolean update(Book book) {
        return bookDao.update(book) > 0;
    }

    public Book selectById(Integer id) {
/*    //模拟业务异常，包装成自定义异常
        if (id == 1){
            throw new BusinessException(Code.BUSINESS_ERR,"艹你输错了，会输不会");
        }
    //模拟系统异常，将可能出现的异常进行包装，转换成自定义异常
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_ERR,"服务器访问超时，请重试",e);
        }*/
        return bookDao.selectById(id);
    }

    public List<Book> getAll() {
        return bookDao.getAll();
    }

    public boolean delete(Integer id) {
        return bookDao.delete(id) > 0;
    }
}
